//
//  SingrecommendTableViewCell.m
//  Music
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SingrecommendTableViewCell.h"

@implementation SingrecommendTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super  initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        //初始化数据源
        self.dataSource = [NSMutableArray  array];
        
        self.flowLayout = [[UICollectionViewFlowLayout  alloc] init];
        self.flowLayout.itemSize = CGSizeMake((Screenwidth-30)/2, 70);
        self.flowLayout.scrollDirection =  UICollectionViewScrollDirectionVertical;
        //最小列间距
        self.flowLayout.minimumInteritemSpacing = 8 ;
        //最小行间距
        self.flowLayout.minimumLineSpacing = 8 ;
        //设置item据四边的间距
        self.flowLayout.sectionInset = UIEdgeInsetsMake(8 , 8 , 8 , 8);
        self.collectionView = [[UICollectionView  alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.bounces = NO;
        self.collectionView.backgroundColor = [UIColor  whiteColor];
        [self.collectionView  registerClass:[SingRecommendCollectionViewCell  class] forCellWithReuseIdentifier:@"Cell"];
        
       [self.contentView  addSubview:_collectionView];
    }
    
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.collectionView.frame =self.contentView.bounds;
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SingRecommendCollectionViewCell  *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:0.05 green:0.36 blue:0.43 alpha:1.00];
    cell.model = self.dataSource[indexPath.row];
    
   
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    smallModel  *model = [self.dataSource  objectAtIndex:indexPath.item];
    NSString  *str  = model.id;
    [self.delegate  passStr:model.name string:str];
       
}
//重写数组set方法:为了刷新数据:
- (void)setDataSource:(NSMutableArray *)dataSource
{
    if (_dataSource != dataSource) {
       _dataSource = dataSource  ;
        [self.collectionView reloadData];
    }
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
